//
//  XYCatalogueController.swift
//  XYReadBook
//
//  Created by fox on 2017/9/28.
//  Copyright © 2017年 tsaievan. All rights reserved.
//
import UIKit
/// 目录 与书签
class XYCatalogueTopView: UIView {

    let bookIndexBtn = UIButton()
    let bookMarkBtn = UIButton()
    var bookIndex:(()->Void)?
    var bookMark:(()->Void)?
    let lineView = UIView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    func setupUI() {
        addSubview(bookIndexBtn)
        addSubview(bookMarkBtn)
        addSubview(lineView)
        lineView.backgroundColor = UIColor.red
        bookIndexBtn.setTitle("目录", for: .normal)
        bookIndexBtn.setTitleColor(UIColor.black, for: .normal)
        bookIndexBtn.setTitleColor(UIColor.red, for: .selected)
        bookMarkBtn.setTitle("书签", for: .normal)
        bookMarkBtn.setTitleColor(UIColor.black, for: .normal)
        bookMarkBtn.setTitleColor(UIColor.red, for: .selected)

        bookIndexBtn.addTarget(self, action: #selector(clickBookIndex(sender:)), for: .touchUpInside)
        bookMarkBtn.addTarget(self, action: #selector(clickBookMark(sender:)), for: .touchUpInside)
    }


    func clickBookIndex(sender:UIButton) {
        sender.isSelected = true
        bookMarkBtn.isSelected = false
        UIView.animate(withDuration: 0.25) {
            self.lineView.centerX = sender.centerX
        }
        bookIndex?()
    }


    func clickBookMark(sender:UIButton) {
        sender.isSelected = true
        bookIndexBtn.isSelected = false
        UIView.animate(withDuration: 0.25) {
            self.lineView.centerX = sender.centerX
        }
        bookMark?()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        let  btnWidth = (self.width / 2) / 3
        // H 30 间距 12
        bookIndexBtn.frame = CGRect.init(x: btnWidth, y: self.height - 42 , width: btnWidth, height: 30)

        let bookMarkX = self.width - (btnWidth * 2)
        bookMarkBtn.frame = CGRect.init(x: bookMarkX, y: bookIndexBtn.y, width: btnWidth, height: 30)

        lineView.frame = CGRect.init(x: bookIndexBtn.x, y: self.height - 3, width: btnWidth, height: 2)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


/// ------------------ 华丽丽的分割线------------------------////
/// TODO 续写点击事件,点击缩放
class XYCatalogueController: XYViewController {

    var clickZoom:(()->Void)?
    var chapters:[Chapters] = [] {
        didSet {
           bookIndexVC.dataArr = chapters
        }
    }


    /// 目录
    var bookIndexVC:XYReadBookIndexController! // 目录


    /// 书签
    var bookMarkVC:XYBookMarkController!
    var topView:XYCatalogueTopView!
    var scrollView:UIScrollView!
    let bookID:String
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }

    deinit {
        XYLog(messsage: "我是目录跟书签死了")
    }


    func setupUI() {
        topView = XYCatalogueTopView()
        weak var weakSelf = self
        topView.bookMark = {
            weakSelf?.scrollView.setContentOffset(CGPoint.init(x: (weakSelf?.view.width)! * 0.8, y: 0), animated: true)
        }


        topView.bookIndex = {
            weakSelf?.scrollView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)
        }

        topView.backgroundColor = UIColor.white
        view.addSubview(topView)
        view.backgroundColor = UIColor.black.withAlphaComponent(0.5)

        let tap = UITapGestureRecognizer.init(target: self, action: #selector(clickViewZoom))
        tap.delegate = self
        view.addGestureRecognizer(tap)
        scrollView = UIScrollView()
        view.addSubview(scrollView)

        bookIndexVC = XYReadBookIndexController.init(bookID: bookID)
        bookMarkVC = XYBookMarkController.init(bookID: bookID)
        addChildViewController(bookIndexVC)
        scrollView.addSubview(bookIndexVC.view)
        addChildViewController(bookMarkVC)
        scrollView.addSubview(bookMarkVC.view)

    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        topView.frame = CGRect.init(x: 0, y: 0, width: ScreenWidth * 0.8, height: 60)
        scrollView.frame = CGRect.init(x: 0, y: topView.frame.maxY, width: topView.width, height: ScreenHeight - 60)
        bookIndexVC.view.frame = CGRect.init(x: 0, y: 0, width: topView.width, height: scrollView.height)
        bookMarkVC.view.frame = CGRect.init(x: topView.width, y: 0, width: topView.width, height: scrollView.height)
    }

    func clickViewZoom() {
        clickZoom?()
    }

    init(bookID:String) {
        self.bookID = bookID
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

// MARK: - 因为刀哥的全屏侧滑,手势,会跟UIScorollView右滑动的时候,有冲突!故此方法
extension XYCatalogueController:UIGestureRecognizerDelegate {

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {

        let bool = touch.view?.isInViewHirerackyOf(view:topView)
        let scrollViewBool = touch.view?.isInViewHirerackyOf(view:scrollView)
        if bool == true || scrollViewBool == true{
            return false
        }else {
            return true
        }
    }

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return false
    }
}
